package com.lagou.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class SimpleOrderManager {

    @Resource
    private MailSender mailSender;

    @Autowired
    private SimpleMailMessage templateMessage;

    public Boolean sendEmail(String targetMailAddress, String content) {

        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
        msg.setTo(targetMailAddress);
        msg.setText(content);
        try{
            this.mailSender.send(msg);
        }
        catch (MailException ex) {
            // simply log it and go on...
            log.error(ex.getMessage());
            return false;
        }
        log.info("===>>> 发送邮件成功, 目标地址: {}, 邮件内容: {}", targetMailAddress, content);
        return true;
    }

}